﻿//TODO: CenterPointReflection: this has to be tested

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// Center Point Reflection, German: Punktspiegelung
    /// </summary>
    public class CenterPointReflection : BaseImageFilter
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="CenterPointReflection"/> class.
        /// </summary>
        public CenterPointReflection()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CenterPointReflection"/> class.
        /// </summary>
        /// <param name="_centerPointReflection">The _center point reflection.</param>
        internal CenterPointReflection(CenterPointReflection _centerPointReflection)
        {
        }

        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bitmapCloned = (UnsafeBitmap)_bitmap.Clone();
            UnsafeBitmap bitmap = _bitmap;

            int nHeight = bitmap.Height;
            int nWidth = bitmap.Width;

            int y = 0;
            int x = 0;

            Color color1 = Color.Transparent;
            Color color2 = Color.Transparent;

            for (y = 0; y < nHeight / 2; y++)
            {
                for (x = 0; x < nWidth / 2; x++)
                {
                    color1 = bitmapCloned.GetPixel(x, y);
                    color2 = bitmapCloned.GetPixel(nWidth - x - 1, nHeight - y - 1);
                    bitmap.SetPixel(x, y, color2);
                    bitmap.SetPixel(nWidth - 1 - x, nHeight - 1 - y, color1);
                }
            }
            for (y = nHeight / 2; y < nHeight; y++)
            {
                for (x = nWidth / 2; x < nWidth; x++)
                {
                    color1 = bitmapCloned.GetPixel(x, y);
                    color2 = bitmapCloned.GetPixel(nWidth - x - 1, nHeight - y - 1);
                    bitmap.SetPixel(x, y, color2);
                    bitmap.SetPixel(nWidth - 1 - x, nHeight - 1 - y, color1);
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new CenterPointReflection(this);
        }
    }
}
